# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community
Edition) available.
Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
"""
# 第一步：优先加载蓝鲸环境变量（必须在导入blueapps之前）
import os
os.environ["BKPAAS_APP_ID"] = "test"  # 临时应用ID，本地开发用
os.environ["BKPAAS_APP_SECRET"] = "test"  # 临时应用密钥
os.environ["BK_PAAS_HOST"] = "http://dev.ce.bktencent.com:8000"  # 与hosts域名匹配

# 第二步：导入其他模块
from config import RUN_VER
from config.default import FRONTEND_BACKEND_SEPARATION
from blueapps.conf.validators import EnvValidator


EnvValidator(RUN_VER).validate()

if RUN_VER == "open":
    from blueapps.patch.settings_open_saas import *  # noqa
else:
    from blueapps.patch.settings_paas_services import *  # noqa

# 本地开发环境
RUN_MODE = "DEVELOP"

# 自定义本地环境日志级别
# from blueapps.conf.log import set_log_level # noqa
# LOG_LEVEL = "DEBUG"
# LOGGING = set_log_level(locals())

# APP本地静态资源目录
STATIC_URL = "/static/"

# APP静态资源目录url
# REMOTE_STATIC_URL = '%sremote/' % STATIC_URL

# Celery 消息队列设置 RabbitMQ
# BROKER_URL = 'amqp://guest:guest@localhost:5672//'
# Celery 消息队列设置 Redis（已修复拼写错误）
BROKER_URL = "redis://localhost:6379/0"

DEBUG = True

# 本地开发数据库设置
# USE FOLLOWING SQL TO CREATE THE DATABASE NAMED APP_CODE
# SQL: CREATE DATABASE `{{ app_code }}` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; # noqa: E501
DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "NAME": "dashuju2",  # noqa
        "USER": "root",
        "PASSWORD": "2005wang",
        "HOST": "localhost",
        "PORT": "3306",
    },
}

# 允许本地访问的主机（包含映射的域名）
ALLOWED_HOSTS = ["localhost", "127.0.0.1", "0.0.0.0", "dev.ce.bktencent.com"]

# 前后端开发模式下支持跨域配置
if FRONTEND_BACKEND_SEPARATION:
    INSTALLED_APPS += ("corsheaders",)
    # 该跨域中间件需要放在前面
    MIDDLEWARE = ("corsheaders.middleware.CorsMiddleware",) + MIDDLEWARE
    CORS_ORIGIN_ALLOW_ALL = True
    CORS_ALLOW_CREDENTIALS = True

# 禁用蓝鲸认证中间件，解决admin循环重定向问题
MIDDLEWARE = [
    mw for mw in MIDDLEWARE
    if not mw.startswith(('blueapps.account.middlewares', 'bkaccount.middlewares'))
]
# 强制使用Django原生认证系统（适配本地登录）
AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend']

# 多人开发时，无法共享的本地配置可以放到新建的 local_settings.py 文件中
# 并且把 local_settings.py 加入版本管理忽略文件中
try:
    from local_settings import *  # noqa
except ImportError:
    pass